﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using EnsureWeb.Web.DataModel;
using Microsoft.Maps.MapControl;

namespace EnsureWeb.UI.ViewModels
{
    public partial class MainViewModel : ViewModelBase
    {
        private Project currentEditProject;
        public Project CurrentEditProject
        {
            get
            {
                return currentEditProject;
            }
            set
            {
                currentEditProject = value;
                RaisePropertyChanged("CurrentEditProject");

                if (value.Id == 0 && value.ProjectDetail == null)
                {
                    value.ProjectDetail = new ProjectDetail();
                    value.AddTime = DateTime.Now;
                }
                else if (value.ProjectDetail == null)
                {
                    this.DomainContext.Load(this.DomainContext.GetProjectDetailQuery().Where(p => p.Id == value.Id));
          
                    this.DomainContext.Load(this.DomainContext.GetProjectOverallQuery().Where(p => p.ProjectId == value.Id));
                }
            }
        }

        public ICommand mapClick;
        public ICommand MapClick
        {
            get
            {
                if (mapClick == null)
                    mapClick = new RelayCommand<MapMouseEventArgs>(e =>
                    {
                        CurrentEditProject.Location = currentEditMap.ViewportPointToLocation(e.ViewportPoint).ToString();
                    });

                return mapClick;
            }
        }

        public ICommand editMapLoaded;
        public ICommand EditMapLoaded
        {
            get
            {
                if (editMapLoaded == null)
                    editMapLoaded = new RelayCommand<object>(e =>
                    {
                        currentEditMap = e as Map;
                    });

                return editMapLoaded;
            }
        }

        private Map currentEditMap;
    }

}